<template>
  <div class="store">
    <div class="pageTitleSubTitle">
      <div class="title">门店管理</div>
      <div class="btn">
        <a-button v-if="configJurisdiction.add" type="primary" @click="openModal"><a-icon type="plus" />添加门店</a-button>
      </div>
    </div>
    <Search v-if="is_search" :search="search" @searchVal="searchVal"/>
    <div class="storeContent">
      <a-table :columns="columns" :data-source="data" :pagination="false" :rowKey="record => record.store_no">
        <span slot="area" slot-scope="area">{{area && area.name?area.name:''}}</span>
        <span slot="store_manager_staff" slot-scope="store_manager_staff">{{store_manager_staff && store_manager_staff.name?store_manager_staff.name:''}}</span>
        <span slot="area_no" slot-scope="area_no,record" class="operation">
          <span v-if="configJurisdiction.update" @click="editColumns(record)">编辑</span>
          <span v-if="configJurisdiction.del" @click="removeStore(record)">删除</span>
        </span>
      </a-table>
      <Pagination :current="page" :total="total" @pageSize="pageSize" />
    </div>
    <a-modal
      v-model="visible"
      :title="title"
      cancelText="关闭"
      okText="保存"
      width="650px">
      <AppendStore v-if="visible" @sendData="getAppendData" ref="child" :appendData="configAppend" />
      <template slot="footer">
        <a-button class="modalCloseBtn" @click="modalChange(1)">关闭</a-button>
        <a-button class="modalConfigBtn" type="primary" :loading="loading" @click="modalChange(2)">保存</a-button>
      </template>
    </a-modal>
  </div>
</template>
<script>
  import Search from '@/common/search/index'
  import Pagination from '@/common/pagination/index'
  import AppendStore from './appendStore'
  import { column, appendData } from './index'
  export default {
    name:'store',
    components:{
      Search,Pagination,AppendStore
    },
    data(){
      return {
        loading:false,
        is_search:false,
        page:1,
        total:500,
        store_no:0,
        visible:false,
        title:'添加门店',
        columns: column,
        configAppend:[],
        data : [],
        searchText:{},
        search: [
          {
            type:'input',
            name:'门店名称',
            place:'请输入名称',
            val:'',
            key:'name',
          },
          {
            type:'input',
            name:'门店编号',
            place:'请输入编号',
            val:'',
            key:'store_no',
          },
          {
            type:'select',
            name:'门店管理员',
            place:'请选择门店',
            val:'',
            key:'store_manager_staff',
            nameKey:'name',
            idKey:'job_no',
            selectData:[]
          }
        ],
        jurisdiction:[
          { key:'admin-store-add', back:'add'},
          { key:'admin-store-update', back:'update'},
          { key:'admin-store-del', back:'del'},
        ],
        configJurisdiction:{}
      }
    },
    created() {
      this.getList();
      this.getSearch();
      this.configJurisdiction = this.getJurisdiction(2,this.jurisdiction);
    },
    methods:{
      pageSize(e){
        this.page = e;
        this.getList();
      },
      getSearch(){
        this.Api.staffs().then(res=>{
          if(res.code !== 200) return;
          this.search.forEach(item=>{
            if(item.key === 'store_manager_staff'){
              item.selectData = res.data
            }
          });
          appendData.forEach(item=>{
            if(item.key === 'store_manager_staff') item.selectData = res.data
          });
          this.is_search = true
        })
      },
      getList(){
        let data = this.searchText;
        data.page = this.page;
        data.size = 10;
        this.Api.stores(data).then(res=>{
          if(res.code !== 200) return;
          this.total = res.data.total;
          this.data = res.data.items
        })
      },
      searchVal(e){
        this.searchText = e;
        this.getList()
      },
      openModal(){
        this.Api.areasAll().then(res=>{
          if(res.code !== 200) return;
          appendData.forEach(item=>{
            item.val = '';
            if(item.key === 'area_no') item.selectData = res.data
          });
          this.title = '添加门店';
          this.configAppend = appendData;
          this.visible = true
        });
      },
      modalChange(e){
        if(e === 1) return this.visible = false;
        this.$refs.child.sendData();
      },
      editColumns(e){
        this.Api.areasAll().then(res=>{
          if(res.code !== 200) return;
          appendData.forEach(item=>{
            item.val = e[item.key];
            if(item.key === 'area_no') item.selectData = res.data;
            if(item.key === 'store_manager_staff') item.val = e.store_manager_staff.job_no
          });
          this.title = '修改门店';
          this.configAppend = appendData;
          this.store_no = e.store_no;
          this.visible = true
        });
      },
      getAppendData(e){
        this.loading = true;
        if(this.title === '添加门店') return this.Api.appendStores(e).then(res=>{
          this.loading = false;
          if(res.code !== 200){
            this.$message.info(res.msg)
          }else{
            this.$message.success(res.msg);
            this.visible = false;
            this.page = 1;
            this.getList()
          }
        });
        this.Api.editStores(e,this.store_no).then(res=>{
          this.loading = false;
          if(res.code !== 200){
            this.$message.info(res.msg)
          }else{
            this.$message.success(res.msg);
            this.visible = false;
            this.page = 1;
            this.getList()
          }
        })
      },
      removeStore(e){
        this.$confirm({
          title: '确定删除当前门店?',
          cancelText: '取消',
          okText:'删除',
          onOk:()=> {
            this.Api.removeStores({},e.store_no).then(res=>{
              if(res.code !== 200) return this.$message.info(res.msg);
              this.$message.info(res.msg);
              this.getList()
            })
          }
        })
      }
    }
  }
</script>
